﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics.Drawables;
using Android.Graphics;
using Android.Content.Res;
using AndroidText = Android.Text;

using BugSense;


namespace MWC.NetcampCheckin
{
    [Activity(Label = "NetcampLastStep", NoHistory = true)]
    public class NetcampLastStep : Activity
    {
        private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0,DateTimeKind.Utc);

        AlarmManager alarmManager;
        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                base.OnCreate(bundle);

                SetContentView(Resource.Layout.NetcampLastStep);

                BugSenseHandler.Instance.ClearCrashExtraData();
                BugSense.Core.Model.LimitedCrashExtraDataList extras = BugSenseHandler.Instance.CrashExtraData;

                extras.Add(new BugSense.Core.Model.CrashExtraData
                {
                    Key = "Screen",
                    Value = "NetcampLastStep"
                });

                //ustaw notifikacje o feedbacku
                var prefs = GetSharedPreferences("NetcampApp", 0);
                if (prefs.GetBoolean("Feedback", false) == false)
                {
                    var prefsEditor = prefs.Edit();
                    prefsEditor.PutBoolean("Feedback", true);
                    prefsEditor.Commit();
                    alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
                    SetFeedbackNotification(DaysManager.GetLastDay().time);
                }

                TextView nameTextView = FindViewById<TextView>(Resource.Id.Name);
                TextView firstText = FindViewById<TextView>(Resource.Id.weAreHappy);
                TextView getSuprise = FindViewById<TextView>(Resource.Id.getSurprise);
                ImageView likeButton = FindViewById<ImageView>(Resource.Id.likeButton);


                //odpowiednio zmodyfikuj wynik w zaleznosci od tego czy jest darowizna lub imie
                bool isDonation = prefs.GetBoolean("Donation", false);
                string userName = prefs.GetString("Name", "");
                string nameString = null;
                if (userName != "")
                {
                    nameString = String.Format("{0} cieszymy się, że jesteś z nami!", userName);
                }
                else
                {
                    nameString = "Cieszymy się, że jesteś z nami!";
                }

                firstText.TextFormatted = AndroidText.Html.FromHtml(nameString);


                TextView[] tabOfNormalItems = new TextView[4];
                int iterator = 0;
                tabOfNormalItems[iterator++] = FindViewById<TextView>(Resource.Id.weAreHappy);
                tabOfNormalItems[iterator++] = FindViewById<TextView>(Resource.Id.distance);
                tabOfNormalItems[iterator++] = FindViewById<TextView>(Resource.Id.Name);
                tabOfNormalItems[iterator++] = FindViewById<TextView>(Resource.Id.getSurprise);

                string Name = "";
                if (isDonation)
                {
                    Name = "Przekazałeś darowiznę na rzecz Spotkań! <b>Dziękujemy!</b>";
                    nameTextView.TextFormatted = AndroidText.Html.FromHtml(Name);
                }
                else
                {
                    getSuprise.Visibility = ViewStates.Gone;
                    nameTextView.Visibility = ViewStates.Gone;
                    likeButton.Visibility = ViewStates.Gone;
                }

                setFont("fonts/texgyreheros-regular-webfont.ttf", tabOfNormalItems, TypefaceStyle.Normal);
            }
            catch (Exception e)
            {
                MWCApp.LogDebug(e);
                NetcampAlerts.UnknownError(this);
            }
        }

        // 1 - sciezka do czcionki, 2 - tablica, 3 - Typ:Bold,Italic,Normal,BoldItalic

        private void setFont(string path, TextView[] tTV, TypefaceStyle Type)
        {
            AssetManager mgr;
            mgr = Assets;
            Typeface font = Typeface.CreateFromAsset(mgr, path);
            for (int i = 0; i < tTV.Length; i++)
            {
                tTV[i].SetTypeface(font, Type);
            }

        }

        private void SetFeedbackNotification(DateTime time)
        {
            time = new DateTime(time.Year, time.Month, time.Day, 20, 0, 0, DateTimeKind.Utc);
            long utcMilis = (long)(time - new DateTime(1970, 1, 1)).TotalMilliseconds;
            Intent feedbackIntent = new Intent(this, typeof(FeedbackNotification));
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, feedbackIntent, PendingIntentFlags.OneShot);
            //do testowania notyfikacji aby bylo dzisiaj
            //long utcMilis = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
            alarmManager.Set(AlarmType.RtcWakeup,utcMilis,pendingIntent);
        }


    }
}